<template>
  <div>
    <el-form
      :inline="true"
      :model="searchMap"
      ref="searchMap"
      class="demo-form-inline"
    >
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="searchMap.code" placeholder="商品编码"></el-input>
      </el-form-item>
      <el-form-item prop="supplierName">
        <el-input
          readonly
          @click.native="handleSearchSupplier"
          v-model="searchMap.supplierName"
          placeholder="供应商"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fencthData">查询</el-button>
        <el-button type="primary" @click="handleAddMember('addMemeberForm')"
          >新增</el-button
        >
        <el-button @click="resetForm('searchMap')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" height="550" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="code" label="商品编码"></el-table-column>
      <el-table-column prop="spec" label="商品规格"></el-table-column>
      <el-table-column prop="retailPrice" label="零售价"></el-table-column>
      <el-table-column prop="purchasePrice" label="进货价"></el-table-column>
      <el-table-column prop="storageNum" label="库存数量"></el-table-column>
      <el-table-column prop="supplierName" label="供应商"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id, 'addMemeberForm')"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog
      :title="formTitle"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form
        ref="addMemeberForm"
        label-width="100px"
        label-position="right"
        style="width:400px"
        :rules="rules"
        :model="newMemberInfo"
      >
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="newMemberInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="newMemberInfo.code"></el-input>
        </el-form-item>
        <el-form-item label="商品规格" prop="spec">
          <el-input v-model="newMemberInfo.spec"></el-input>
        </el-form-item>
        <el-form-item label="零售价" prop="retailPrice">
          <el-input v-model="newMemberInfo.retailPrice"></el-input>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input v-model="newMemberInfo.purchasePrice"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="storageNum">
          <el-input v-model="newMemberInfo.storageNum"></el-input>
        </el-form-item>
        <el-form-item v-show="false" label="供应商ID" prop="supplierID">
          <el-input v-model="newMemberInfo.supplierID"></el-input>
        </el-form-item>
        <el-form-item
          readonly
          @click.native="handleEdiySearchSupplier"
          label="供应商"
          prop="supplierName"
        >
          <el-input v-model="newMemberInfo.supplierName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancleForm('addMemeberForm')">取 消</el-button>
        <el-button
          type="primary"
          @click="
            newMemberInfo.id === ''
              ? addMember('addMemeberForm')
              : updateMember('addMemeberForm')
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog :visible.sync="supplierDialogFormVisible" width="500px">
      <supplier @option-supplier="optionSupplier" :isDialog="true"></supplier>
    </el-dialog>
  </div>
</template>
<script>
import OpApi from "@/api/baseSCUD";
import Supplier from "@/views/Supplier";
const resource = "goods";
const cresource = "商品";

export default {
  components: { Supplier },
  data() {
    return {
      list: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      formTitle: "新增" + cresource,
      searchMap: {
        name: "",
        code: "",
        supplierName: "",
        supplierID: ""
      },
      dialogFormVisible: false,
      supplierDialogFormVisible: false,
      supplierDialogFormVisibleSearch: true,
      newMemberInfo: {
        id: "",
        name: "",
        code: "",
        spec: "",
        retailPrice: "",
        purchasePrice: "",
        storageNum: "",
        supplierName: "",
        supplierID: ""
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入" + cresource + "名称",
            trigger: "blur"
          }
        ],
        code: [{ required: true, message: "请输入商品编码", trigger: "blur" }],
        supplierName: [
          { required: true, message: "请输入商品供应商", trigger: "blur" }
        ],
        supplierID: [
          { required: true, message: "请输入商品供应商", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    fencthData() {
      OpApi.search(
        resource,
        this.currentPage,
        this.pageSize,
        this.searchMap
      ).then(response => {
        console.log(response.data);
        const respData = response.data;
        if (respData.flag) {
          this.total = respData.data.total;
          this.list = respData.data.rows;
          console.log(this.list);
        } else {
          this.$message({
            message: response.data.message,
            type: "warning",
            duration: 1000
          });
        }
      });
    },
    handleSearchSupplier() {
      this.supplierDialogFormVisibleSearch = true;
      this.supplierDialogFormVisible = true;
    },
    handleEdiySearchSupplier() {
      this.supplierDialogFormVisibleSearch = false;
      this.supplierDialogFormVisible = true;
    },
    optionSupplier(supplier) {
      if (this.supplierDialogFormVisibleSearch) {
        this.searchMap.supplierName = supplier.name;
        this.searchMap.supplierID = supplier.id;
      } else {
        this.newMemberInfo.supplierName = supplier.name;
        this.newMemberInfo.supplierID = supplier.id;
      }

      this.supplierDialogFormVisible = false;
    },
    handleEdit(id, formName) {
      this.dialogFormVisible = true;
      this.formTitle = "" + cresource + "编辑";
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      OpApi.getById(resource, id).then(response => {
        if (response.data.flag) {
          this.newMemberInfo = response.data.data;
          console.log(this.newMemberInfo);
        } else {
          this.$message({
            message: response.data.message,
            type: "warning",
            duration: 1000
          });
        }
      });
    },
    handleDelete(id) {
      console.log(id);
      this.$confirm("此操作将删除该" + cresource + "信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          OpApi.delById(resource, id).then(response => {
            if (response.data.flag) {
              this.fencthData();
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fencthData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.fencthData();
    },
    resetForm(formName) {
      console.log("resetForm:", formName);
      this.$refs[formName].resetFields();
    },
    cancleForm(formName) {
      console.log("cancleForm:", formName);
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    },
    handleAddMember(formName) {
      this.dialogFormVisible = true;
      this.formTitle = "新增" + cresource + "";
      console.log("this.newMemberInfo.id", this.newMemberInfo.id);
      console.log("this.newMemberInfo.id", this.newMemberInfo.id === "");
      console.log("this.newMemberInfo.id", this.newMemberInfo.id === null);
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      // this.$refs[formName].resetFields();
    },
    addMember(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return;
        } else {
          console.log("addMember resetForm:", formName);
          OpApi.add(resource, this.newMemberInfo).then(response => {
            console.log(response.data);
            const respData = response.data;
            if (respData.flag) {
              this.$message({
                message: "添加" + cresource + "Success",
                type: "success",
                duration: 1000
              });
              this.dialogFormVisible = false;
              this.$refs[formName].resetFields();
              this.fencthData();
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        }
      });

      // this.$refs[formName].resetFields();
    },
    updateMember(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return;
        } else {
          console.log("updateMember resetForm:", formName);
          OpApi.update(resource, this.newMemberInfo).then(response => {
            console.log(response.data);
            const respData = response.data;
            if (respData.flag) {
              this.$message({
                message: "" + cresource + "更新Success",
                type: "success",
                duration: 1000
              });
              this.dialogFormVisible = false;
              this.$refs[formName].resetFields();
              this.fencthData();
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        }
      });

      // this.$refs[formName].resetFields();
    }
  },
  created() {
    this.fencthData();
  }
};
</script>
<style scoped>
.el-form {
  margin-top: 20px;
}
</style>
